/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package batailledesardennes.game.entity;

import batailledesardennes.game.GameConstants;
import batailledesardennes.game.GameEnv;
import batailledesardennes.game.entity.collision.EntityCollsionManager;
import batailledesardennes.game.entity.drawer.BaseEntityDrawer;
import batailledesardennes.game.interfaces.Drawable;
import batailledesardennes.game.interfaces.Updatable;
import batailledesardennes.util.geomFloat.Point;
import batailledesardennes.util.geomFloat.Rectangle;
import batailledesardennes.util.geomFloat.Shape;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 *
 * @author charly
 */
public class BuildingEntity extends BaseEntity implements Drawable, Updatable {

    protected String imageFile;
    protected Point entryPoint;
    protected List<Entity> insideEntities;
    protected int buildingId;
    protected BaseEntityDrawer entityDrawer;
    protected EntityCollsionManager collsionManager;

    public BuildingEntity(String imageFile, Shape bounds, Point entry, Point imageOffset, int buildingId) {
        this.imageFile = imageFile;
        this.entryPoint = entry;
        this.buildingId = buildingId;
        
        entityDrawer = new BaseEntityDrawer();
        entityDrawer.setEntity(this);
        entityDrawer.setImageOffset(imageOffset);
        
        collsionManager = new EntityCollsionManager();
        collsionManager.setEntity(this);
        collsionManager.setBounds(bounds);
        
        insideEntities = new ArrayList<Entity>();
        pos = new Point();
        selectable = false;
        collidable = true;
        drawable = true;
        entityTypeId = GameConstants.ENTITY_TYPE_ID_BUILDING;
    }

    @Override
    public void register(GameEnv env) {
        this.env = env;
        env.getEntitiesManager().addEntity(this);
        env.getUpdatableManager().addUpdatable(this);
        try {
            Image image = new Image(imageFile);
            entityDrawer.setImage(image);
        } catch (SlickException ex) {
            Logger.getLogger(BuildingEntity.class.getName()).log(Level.SEVERE, null, ex);
        }
        collsionManager.register();
    }

    @Override
    public void unregister() {
        env.getEntitiesManager().removeEntity(this);
        env.getUpdatableManager().removeUpdatable(this);
        collsionManager.unregsiter();
    }

    @Override
    public short getDrawablePriority() {
        return GameConstants.DRAWABLE_PRIORITY_FRONT;
    }

    @Override
    public void update(int delta) {
    }

    @Override
    public void draw(Graphics g) {
        entityDrawer.draw(g);
    }

    public int getBuildingId() {
        return buildingId;
    }

    public Point getEntryPoint() {
        return entryPoint.getCopy().translate(pos.getX(), pos.getY());
    }

    public List<Entity> getInsideEntities() {
        return insideEntities;
    }

    @Override
    public Rectangle getBounds() {
        Rectangle boundsRect = collsionManager.getBounds().getBoundsRect();
        return collsionManager.getBounds().getBoundsRect();
    }
}
